<?php

namespace backend\modules\user\controllers;

use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\models\user\Comm;
use common\enums\StatusEnum;
use backend\controllers\BaseController;
/**
 * 客户端
 *
 * Class ClientController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class CommController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Comm::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['title'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);
       // var_dump(Yii::$app->request->queryParams);die();
        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);
        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
            'menuDropDownList' => Yii::$app->services->menu->gettype(),
        ]);
    }

    /**编辑和新增
     * @return mixed|string|\yii\web\Response
     * @throws \yii\base\ExitException
     *
     */
    public function actionAjaxEdit()
    {
        $id = Yii::$app->request->get('id');
        $model = $this->findModel($id);
        $model->img = explode(',',$model->img);
        //查询其他表

//
//        var_dump($model['img']);die();
//        var_dump($model->load(Yii::$app->requeimgst->post()));die();
        // ajax 校验


        if ($model->load(Yii::$app->request->post())) {
            $arr = implode(',',$model['img']);
            $model->img = $arr;
            return $model->save()
                ? $this->redirect(Yii::$app->request->referrer)
                : $this->message($this->getError($model), $this->redirect(Yii::$app->request->referrer), 'error');
        }

        return $this->renderAjax($this->action->id, [
            'model' => $model,
            'menuDropDownList' => Yii::$app->services->menu->gettype(),
        ]);
    }

}